package com.siyoumi.app.modules.app_book.admin;

import com.siyoumi.app.entity.BookSku;
import com.siyoumi.app.modules.app_book.service.SvcBookSku;
import com.siyoumi.app.modules.app_book.vo.VaBookSkuDay;
import com.siyoumi.component.XApp;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.util.XDate;
import com.siyoumi.util.XReturn;
import com.siyoumi.validator.XValidator;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/xadmin/app_book/app_book__sku_day__edit")
public class app_book__sku_day__edit
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        String skuId = input("sku_id");
        XValidator.isNull(skuId, "sku_id miss");
        BookSku entitySku = SvcBookSku.getApp().getEntity(skuId);
        XValidator.isNull(entitySku, "sku_id 异常");
        setPageTitle(entitySku.skuName() + "-批量操作");

        Map<String, Object> data = new HashMap<>();
        data.put("bskud_sku_id", skuId);
        data.put("bspud_add_price", entitySku.getBsku_day_price());
        data.put("bspud_add_price_private", entitySku.getBsku_day_price_private());
        data.put("week", List.of(1, 2, 3, 4, 5, 6, 7));
        if (isAdminEdit()) {
            //BookSpu entity = SvcBookSpu.getApp().loadEntity(getID());
            //
            //HashMap<String, Object> dataAppend = new LinkedHashMap<>();
            //dataAppend.put("id", entity.getKey());
            ////
            ////合并
            //data = entity.toMap();
            //data.putAll(dataAppend);
        }
        getR().setData("data", data);

        //星期
        setPageInfo("weeks", XApp.getWeeks());
        setPageInfo("can_edit", SvcBookSku.getBean().canEdit(entitySku));

        return getR();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/save")
    public XReturn save(@Validated() VaBookSkuDay vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result);
        //
        BookSku entitySku = SvcBookSku.getApp().getEntity(vo.getBskud_sku_id());
        if (entitySku == null) {
            result.addError(XValidator.getErr("bspud_add_price", "SKU ID异常"));
        }
        if (vo.getBspud_add_price().compareTo(entitySku.getBsku_day_price()) < 0) {
            result.addError(XValidator.getErr("bspud_add_price", "不能小于SKU价格," + entitySku.getBsku_day_price().toString()));
        }
        if (vo.getBspud_add_price_private().compareTo(entitySku.getBsku_day_price_private()) < 0) {
            result.addError(XValidator.getErr("bspud_add_price_private", "不能小于SKU价格," + entitySku.getBsku_day_price_private().toString()));
        }
        if (vo.getBspud_add_price().compareTo(BigDecimal.ZERO) <= 0) {
            result.addError(XValidator.getErr("bspud_add_price", "拼房价不能小于0"));
        }
        if (vo.getBspud_add_price_private().compareTo(vo.getBspud_add_price()) < 0) {
            result.addError(XValidator.getErr("bspud_add_price_private", "包房价要大于拼房价"));
        }
        if (vo.getBegin_date().isBefore(XDate.today())) {
            result.addError(XValidator.getErr("end_date", "开始日期要大于或者等于当天"));
        }
        XValidator.getResult(result);
        //
        return SvcBookSku.getBean().dayEditBatch(vo);
    }
}